package com.tencreat.service.pt.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.domain.pt.MallPtUsers;
import com.tencreat.mapper.pt.MallPtUsersMapper;
import com.tencreat.service.pt.IMallPtUsersService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 拼团详情Service业务层处理
 *
 * @author tencreat
 * @date 2021-11-08
 */
@Service
public class MallPtUsersServiceImpl extends ServiceImpl<MallPtUsersMapper, MallPtUsers> implements IMallPtUsersService {


    /**
     * 查询拼团详情
     *
     * @param id 拼团详情ID
     * @return 拼团详情
     */
    @Override
    public MallPtUsers selectMallPtUsersById(ReqIdParam id) {
        return baseMapper.selectById(id.getId());
    }

    /**
     * 查询拼团详情列表
     *
     * @param mallPtUsers 拼团详情
     * @return 拼团详情
     */
    @Override
    public List<MallPtUsers> selectMallPtUsersList(MallPtUsers mallPtUsers) {
        return baseMapper.selectList(Wrappers.query(mallPtUsers));
    }

    /**
     * 新增拼团详情
     *
     * @param mallPtUsers 拼团详情
     * @return 结果
     */
    @Override
    public int insertMallPtUsers(MallPtUsers mallPtUsers) {
        return baseMapper.insert(mallPtUsers);
    }

    /**
     * 修改拼团详情
     *
     * @param mallPtUsers 拼团详情
     * @return 结果
     */
    @Override
    public int updateMallPtUsers(MallPtUsers mallPtUsers) {
        mallPtUsers.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(mallPtUsers);
    }

    /**
     * 批量删除拼团详情
     *
     * @param ids 需要删除的拼团详情ID
     * @return 结果
     */
    @Override
    public int deleteMallPtUsersByIds(ReqIdsParam ids) {
        return baseMapper.deleteBatchIds(ids.getId());
    }

    /**
     * 删除拼团详情信息
     *
     * @param id 拼团详情ID
     * @return 结果
     */
    @Override
    public int deleteMallPtUsersById(ReqIdParam id) {
        return baseMapper.deleteById(id.getId());
    }

    @Override
    public Page<Map<String, Object>> lotteryUsersList(Page<Map<String, Object>> page, Integer ptId, Long uuid, String phone, Integer type) {
        Page<Map<String, Object>> list = baseMapper.lotteryUsersList(page, ptId, uuid, phone, type);
        list.getRecords().forEach(i -> {
            i.put("joinCount", this.lambdaQuery().eq(MallPtUsers::getUuid, i.get("uuid")).count());
            i.put("lotteryCount", this.lambdaQuery().eq(MallPtUsers::getUuid, i.get("uuid")).eq(MallPtUsers::getIsLottery, 1).count());
        });
        return list;
    }

    @Override
    public Page<Map<String, Object>> ptHistoricalRecord(Page<Map<String, Object>> page, Integer goodsId,Date time, Integer period) {
        Page<Map<String, Object>> record = baseMapper.ptHistoricalRecord(page, goodsId, time, period);
        record.getRecords().forEach(i -> {
            i.put("lotteryCount",this.lambdaQuery().eq(MallPtUsers::getPtId, i.get("ptId")).eq(MallPtUsers::getIsLottery, 1).count());
        });
        return record;
    }

    @Override
    public Page<Map<String, Object>> ptDetail(Page<Map<String, Object>> page, Integer ptId) {
        Page<Map<String, Object>> detail = baseMapper.ptDetail(page, ptId);
        detail.getRecords().forEach(i -> {
            i.put("joinCount", this.lambdaQuery().eq(MallPtUsers::getUuid, i.get("uuid")).count());
            i.put("lotteryCount", this.lambdaQuery().eq(MallPtUsers::getUuid, i.get("uuid")).eq(MallPtUsers::getIsLottery, 1).count());
        });
        return detail;
    }

    @Override
    public Map<String, Object> lotteryDetailInfo(Integer ptId) {
        Map<String, Object> map = baseMapper.lotteryDetailInfo(ptId);
        Integer win = lambdaQuery().eq(MallPtUsers::getPtId, ptId).eq(MallPtUsers::getInternalLottery, 1).count();
        Integer selected = lambdaQuery().eq(MallPtUsers::getPtId, ptId).eq(MallPtUsers::getInternalLottery, 2).count();
        Integer loser = lambdaQuery().eq(MallPtUsers::getPtId, ptId).eq(MallPtUsers::getInternalLottery, 3).count();
        map.put("win",win);
        map.put("selected",selected);
        map.put("loser",loser);
        return map;
    }
}
